package com.github.zxhtom.excel.expose;

import com.github.zxhtom.excel.Excel;
import com.github.zxhtom.excel.constant.HeadConstant;
import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.*;
import jxl.write.biff.RowsExceededException;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;

/**
 * @package com.github.zxhtom.excel.expose
 * @Class JXL
 * @Description TODO
 * @Author zhangxinhua
 * @Date 19-10-31 下午4:10
 */
public class JXLTools<T> implements Excel<T> {

    @Override
    public void readData() {

    }

    @Override
    public void export(List<T> list) {
        String parentFileName = EasyExcelTools.class.getResource("/").getPath() + "student/jxl/";
        File file = new File(parentFileName);
        if (!file.exists()) {
            file.mkdirs();
        }
        String fileName = parentFileName + System.currentTimeMillis() + ".xlsx";
        String head[] = new String[]{HeadConstant.FIRSTNAME,HeadConstant.SECONDNAME};
        String sixHead[] = new String[]{"学号","姓名","年龄","性别","生日","身高"};
        String sixField[] = new String[]{"id","userName","age","sex","birth","height"};
        //创建工作部
        WritableWorkbook wwb;
        try {
             wwb = Workbook.createWorkbook(new File(fileName));
             //设置样式
            WritableCellFormat font = setStyle();
            //创建工作页
            WritableSheet sheet = wwb.createSheet(HeadConstant.FIRSTNAME, 0);
            Integer currentRowIndex = 0;
            for (String headName : head) {
                //col ,row , col , row
                sheet.mergeCells(0, currentRowIndex,  sixHead.length-1,currentRowIndex);
                Label headLabel = new Label(0, currentRowIndex++, headName,font);
                sheet.addCell(headLabel);
                //合并单元格
            }
            for (int i = 0; i < sixHead.length; i++) {
                Label headLabel = new Label(i, currentRowIndex, sixHead[i],font);
                sheet.addCell(headLabel);
            }
            currentRowIndex++;
            //填充数据
            for (T item : list) {
                Class<?> clazz = item.getClass();
                for (int i = 0; i < sixField.length; i++) {
                    try {
                        Field field = clazz.getDeclaredField(sixField[i]);
                        field.setAccessible(true);
                        Object o = field.get(item);
                        Label dataLabel = new Label(i, currentRowIndex, o.toString(),font);
                        sheet.addCell(dataLabel);
                    } catch (NoSuchFieldException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
                currentRowIndex++;
            }
            //写出文件并关闭流
            wwb.write();
            wwb.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (RowsExceededException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        }

    }

    private WritableCellFormat setStyle() {
        WritableFont titleFont = new WritableFont(jxl.write.WritableFont.createFont("黑体"), 12,
                WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
        WritableCellFormat twcf = new WritableCellFormat(titleFont);
        try {
            twcf.setAlignment(Alignment.CENTRE);// 把水平对齐方式指定为居中
            twcf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 把垂直对齐方式指定为居中
            twcf.setWrap(true);// 通过调整宽度和高度自动换行
        } catch (WriteException e) {
            e.printStackTrace();
        }
        return twcf;
    }

}
